<?php

/*
	PIE Content Management System
	John Girata
	Licensed under the GNU General Public License version 3
*/

defined('PIESECVAR') or die('Restricted Access');

class ModuleHandler
{
	private $last_content = '';
	
	function parse(&$html)
	{
		// IDs must start with MOD and only contain uppercase letters, numbers, and underscores
		$pattern = '/MOD[0-9A-Z_]+/';
		
		preg_match_all($pattern, $html, $matches);
		$matches = $matches[0];
		
		foreach($matches as $call)
			$html = preg_replace("/<%%$call%%>/", $this->load($call), $html);
	}
	
	function load($pos)
	{
		global $db;
		
		$db->rescape($pos);
		$db->query("SELECT * FROM `#__modules` WHERE `pos`='$pos' ORDER BY `order`");
		
		$modules = $db->get_array();
		
		$content = '';
		
		foreach($modules as $module)
		{
			include(MOD_DIR . $module['id'] . '/' . $module['id'] . '.php');
			$content .= $this->last_content;
		}
		
		return $content;
	}		
			
	function set($content)
	{
		$this->last_content = $content;
	}
}

?>